﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBControl;
using System.Text;

namespace ProjectX
{
    public partial class repass : Errorbase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void enter_Click(object sender, ImageClickEventArgs e)
        {
            string getemail = "SELECT email FROM UserRegister WHERE userName='" + this.username.Text + "'";
            SqlDbHelper sdh = new SqlDbHelper();
            string email = sdh.ExecuteScalar(getemail).ToString();
            if (email == "")
            {
                this.usernameValidator.IsValid = false;
                this.usernameValidator.ErrorMessage = "邮件发送失败，请检查您的用户名是否正确。";
            }
            else
            {
                emailClass ec = new emailClass();
                StringBuilder emailmsg = new StringBuilder();
                string randomstr = seRandNum(10);
                string updateUserRandom = "UPDATE UserRegister SET passRandom = '" + randomstr + "'";
                sdh.ExecuteNonQuery(updateUserRandom);
                emailmsg.Append("<div>这是淘我想淘网发送的密码修改邮件，请点击下面的链接进行修改密码操作，如无法打开链接请拷贝链接地址到浏览器中打开。</div>");
                emailmsg.Append("<div><a target='_blank' href='http://localhost:9527/rePass/" + this.username.Text + "/" + randomstr + ".html'>http://localhost:9527/rePass/" + this.username.Text + "/" + randomstr + ".html</a></div>");
                emailmsg.Append("<div><font color='red'>该邮件为系统发送邮件，请勿回复，谢谢！</font></div>");
                ec.Sendmail(this.username.Text, email, "淘我想淘网", "淘我想淘网找回密码", emailmsg);
            }
        }

        private static char[] constant =   
        {   
        '0','1','2','3','4','5','6','7','8','9', 
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',   
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'   
        };

        private string seRandNum(int Length)
        {
            StringBuilder newRandom = new StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(62)]);
            }
            return newRandom.ToString();
        }
    }
}